<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>回答问题</title>
    <script type="text/javascript" src="js/jquery-3.4.1.js"></script>
</head>
<body>
<div id="look" style="margin:20px auto;  display:table;">
    <table cellspacing="0" cellpadding="0" width="250px">
        <tr>
            <td width="80px">问题:</td>
            <td><span style="background: #93ee95" id="title"></span></td>
        </tr>
        <tr>
            <td>问题描述:</td>
            <td><span style="background: #93ee95" id="detailDesc"></span></td>
        </tr>
        <tr>
            <td>网友回答:</td>
            <td id="answer"></td>
        </tr>
    </table>

</div>
<div style="margin:20px auto;  display:table;">
    <form id="form">
        <input type="hidden" id="qid" name="qid">
        <p>我来回答:<textarea rows="3" cols="20" id="ansContent" name="ansContent"></textarea></p>
        <p><input type="submit" value="提交答案" id="submit"><a href="index.html">返回首页</a></p>
    </form>
</div>
</body>
<script type="text/javascript">
    $(function () {
        var qid = location.search.substring(1);
        $("#qid").val(qid);
        $.get("getBy", {id: qid}, function (p) {
            var data = p.data;
            $("#title").html(data.title);
            $("#detailDesc").html(data.detailDesc);
        });

        var t = $("#look");
        $.get("getAnswer", {qid: qid}, function (data) {
            var list = data.data;
            $.each(list, function (i, d) {
                $("#answer").append("<div style='background: #93ee95'><p>" + d.ansDate + "</p><p>" + d.ansContent + "</p></div>")
            })
        });

        $("#submit").click(function () {
            var ansContent = $("#ansContent").val();
            if (ansContent.trim() == "") {
                alert("必须填写您的答案");
                return false;
            }
            $.post("addAnswer", {"qid": qid, "ansContent": ansContent}, function (p) {
                if (p.code == 0) {
                    alert(p.msg);
                    $(location).prop("href", "index.html");
                    return false;
                }
                alert(p.msg);
            })
            return false;
        });
    });
</script>
</html>